<?php
    // 注册后端

    $username = $_POST['username'];
    $password = $_POST['password'];
    $nickname = $_POST['nickname'];

    // 验证用户名是否存在
    $link = mysqli_connect('localhost','root','root','helianna');

    $sql= "SELECT * FROM `user` WHERE `username`='$username'";

    $res = mysqli_query($link,$sql);

    $data = mysqli_fetch_all($res,MYSQLI_ASSOC);

    // 判断不存在的话将数据写入,返回注册成功,存在的话返回注册失败
    if(count($data)){
        $arr = [
            "message" => "注册失败,用户名已被占中",
            "code" => 0
        ];

        echo json_encode($arr);

        exit;
    }


    $sql1 = "INSERT INTO `user` (`username`, `password`, `nickname`) VALUES('$username', '$password', '$nickname')";

    $res1 = mysqli_query($link, $sql1);

    if($res1){
        $arr = [
            "message" => "注册成功",
            "code" =>1 
        ];
    }else{
        $arr = [
            "message" => "注册失败,请联系管理员",
            "code" =>2 
        ];
    }

    echo json_encode($arr);


?>